'''生词本是一款专门为用户背单词而设计的软件,它有背单词,添加新单词.删除单词,查找单词,清空生词本,退出生词本这些基本功能.
   本案例要求编写代码,实现一个基于面向对象思想的,具有上述功能的生词本程序'''
class Words:
    def __init__(self):
        self.words_li = []
    def welcom(self):
        while True:
            print('1.背单词')
            print('2.添加新单词')
            print('3.删除单词')
            print('4.查找单词')
            print('5.清空生词本')
            print('6.退出生词本')
            option = input('请输入想选择的功能编号:\n')
            if option == '1':
                self.show_word()
            elif option == '2':
                self.add_word()
            elif option == '3':
                self.del_word()
            elif option == '4':
                self.seek_word()
            elif option == '5':
                self.del_all()
            elif option == '6':
                break

    def show_word(self):
        if len(self.words_li) == 0:
            print('生词本为空')
        elif len(self.words_li) != 0:
            for i in self.words_li:
                print(i)
        return self.words_li

    def add_word(self):
        dict_word = dict()
        word = input('请输入英文单词:\n')
        mean = input('请输入单词的中文意思:\n')
        dict_word[word] = mean
        self.words_li.append(dict_word)
        return self.words_li

    def del_word(self):
        if len(self.words_li) == 0:
            print('生词本为空')
        elif len(self.words_li) != 0:
            self.show_word()
            new_list = []
            word = input('请输入需要删除的单词')
            removed = False
            for word_dict in self.words_li:
                if word in word_dict:
                    removed = True
                    continue
                new_list.append(word_dict)
            self.words_li = new_list
            if removed == True:
                print('删除成功')
            else:
                print('要删除的单词不存在')
        return self.words_li

    def seek_word(self):
        if len(self.words_li) == 0:
            print('生词本为空')
        elif len(self.words_li) != 0:
            word = input('请输入需要查找的单词:\n')
            for i in self.words_li:
                if word in i:
                    print(i)
                else:
                    print('查找的单词不存在')

    def del_all(self):
        self.words_li = []
        print('单词本已清空')
        return self.words_li

if __name__ == '__main__':
    words = Words()
    words.welcom()